package quick.portal.fragment.email;


import quick.portal.R;
import quick.view.tabstrip.PagerSlidingTabStrip;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class EmailMainFragment extends Fragment {

	private static String[] TITLES = new String[]{"未读邮件","收件箱","发件箱","垃圾箱"};

    private PagerSlidingTabStrip tabs = null;
    private ViewPager pager = null;
    
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.email_main_fragment, container,false);
		
		this.tabs = (PagerSlidingTabStrip)view.findViewById(R.id.email_tabs);
		this.pager = (ViewPager)view.findViewById(R.id.email_pager);
		
		return view;
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		FragmentPagerAdapter adapter = new EmialAdapter(getChildFragmentManager());
        pager.setAdapter(adapter);

        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        pager.setPageMargin(pageMargin);

        tabs.setViewPager(pager);
	}
	
	public class EmialAdapter extends FragmentPagerAdapter {
		
	        public EmialAdapter(FragmentManager fm) {
	            super(fm);
	        }

	        @Override
	        public Fragment getItem(int position) {
	        	
	            if (position == 0) {
	                return new EmailUnreadFragment();
	            }
	            if (position == 1) {
	                return new EmailInboxFragment();
	            }
	            if (position == 2) {
	                return new EmailOutboxFragment();
	            }
	            if (position == 3) {
	                return new EmailDustbinFragment();
	            }
	            
	            return new EmailUnreadFragment();
	            
	        }

	        @Override
	        public CharSequence getPageTitle(int position) {
	            return TITLES[position % TITLES.length];
	        }

	        @Override
	        public int getCount() {
	            return TITLES.length;
	        }
	    }

}
